import { Button, Modal } from 'antd';
import { inject, observer } from "mobx-react";
import { toJS } from 'mobx';
import { WeaDialog, WeaFormItem, WeaSearchGroup, WeaNewScroll, WeaRightMenu, WeaTab } from 'ecCom';
import { WeaSwitch, WeaTableNew } from 'comsMobx';
const WeaTable = WeaTableNew.WeaTable;
import { WeaLocaleProvider } from 'ecCom';
const getLabel = WeaLocaleProvider.getLabel;
import "./style/inventory.less";
@observer
class PlanListInfoDialog extends React.Component {
    constructor(props){
        super(props);
        this.formParams = {};
        this.formParams1 = {};
    }

    render() {
        const { visible1, basicInfo1, setVisiable, listtitle, selectid, maintitle, sessionkey_planlist, plantableStore1, sessionkey_planlist_add, plantableStore2,
            addCptVisible, cptSerchform, doCptSearch, listid, showcptSearchAd, showcptAddSearchAd, clearCptAddFormFields, setCptAddSearchAd, doCptAdd, doCptAddAll } = this.props.contentStore;
        const selectedLen = plantableStore2.selectedRowKeys.length;

        const getAdButtons = (listStore) => {
            const { handlePlanList, setCptSearchAd, clearCptFormFields } = listStore;
            return [
                (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@cnubg7@search`} type="primary" onClick={() => { handlePlanList(true, { id: listid, name: listtitle }, 0); setCptSearchAd(false); }}>{getLabel(197, "搜索")}</Button>),
                (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@76p2ox@reset`} type="ghost" onClick={() => { clearCptFormFields(); }}>{getLabel(2022, "重置")}</Button>),
                (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@r5089f@close`} type="ghost" onClick={() => { setCptSearchAd(false) }}>{getLabel(201, "取消")}</Button>)
            ];
        }

        const getCptAdButtons = (listStore) => {
            const { handlePlanList, setCptSearchAd, clearCptFormFields } = listStore;
            return [
                (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@p5yt92@search`} type="primary" onClick={() => { doCptSearch({ listid: listid }) }}>{getLabel(197, "搜索")}</Button>),
                (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@3cq9si@reset`} type="ghost" onClick={() => { clearCptAddFormFields(); }}>{getLabel(2022, "重置")}</Button>),
                (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@w5nggv@close`} type="ghost" onClick={() => { setCptAddSearchAd(false) }}>{getLabel(201, "取消")}</Button>)
            ];
        }

        let dialogHeight = window.innerHeight;
        let dialogWidth = 1200;
        if (dialogHeight > 768) {
            dialogHeight = 600;
        }else{
            dialogHeight = 500;
            dialogWidth = 1000;
        }
        return (
            <div>
                <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@bsfxbo`}
                    title={listtitle}
                    visible={visible1}
                    icon="icon-coms-fa"
                    iconBgcolor="#f14a2d"//this.props.contentStore.handleDialog(true, { id: selectid, name: maintitle }, 0);
                    onCancel={() => { this.props.contentStore.setVisiable(false, 7) }}
                    style={{ width: dialogWidth, height: dialogHeight }}
                >
                    <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@utudxp`} datas={this.getRightMenu()}  >
                        <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@6vxy3p`} //高级搜素
                            buttonsAd={getAdButtons(this.props.contentStore)}
                            searchType={['advanced']}
                            showSearchAd={showcptSearchAd}
                            setShowSearchAd={bool => this.props.contentStore.setCptSearchAd(bool)}
                            hideSearchAd={()=> this.props.contentStore.setCptSearchAd(false)}
                            onSearch={v => { this.props.contentStore.doSearch() }}
                            showSearchDrop={false}
                            searchsAd={<div>{this.getCptCondition()}</div>}
                            buttons={this.getTopButtons()}
                        />
                        <div className='cpt-dialog-form'>
                            {this.getFormFields()}
                        </div>
                        <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@tslciu`}
                            sessionkey={sessionkey_planlist}
                            comsWeaTableStore={plantableStore1}
                            outerHeight={dialogHeight}
                            hasOrder={true}
                            needScroll={true}
                            onOperatesClick={this.onOperatesClick} />
                    </WeaRightMenu>
                </WeaDialog>

                <WeaDialog ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@hzaklg`}
                    title={getLabel(83476, '添加') + getLabel(535, '资产')}
                    visible={addCptVisible}
                    onCancel={() => { setVisiable(false, 5) }}
                    icon='icon-coms-fa'
                    iconBgcolor='#f14a2d'
                    style={{ width: dialogWidth, height: dialogHeight }}
                >
                    <WeaRightMenu ecId={`${this && this.props && this.props.ecId || ''}_WeaRightMenu@jhcp7o`} datas={this.getRightMenu1()}  >
                            <WeaTab ecId={`${this && this.props && this.props.ecId || ''}_WeaTab@fwemyz`} //高级搜素
                                buttonsAd={getCptAdButtons(this.props.contentStore)}
                                searchType={['advanced']}
                                showSearchAd={showcptAddSearchAd}
                                setShowSearchAd={bool => this.props.contentStore.setCptAddSearchAd(bool)}
                                hideSearchAd={()=> this.props.contentStore.setCptAddSearchAd(false)}
                                onSearch={v => { this.props.contentStore.doCptSearch({ listid: listid }) }}
                                showSearchDrop={false}
                                searchsAd={<div>{this.getCondition()}</div>}
                                buttons={this.getTopButtons1()}
                            />
                            <WeaTable ecId={`${this && this.props && this.props.ecId || ''}_WeaTable@debox7`}
                                sessionkey={sessionkey_planlist_add}
                                comsWeaTableStore={plantableStore2}
                                hasOrder={true}
                                needScroll={true}
                                outerHeight={dialogHeight-50}
                            />
                    </WeaRightMenu>
                </WeaDialog>
            </div>)
    }

    getTopButtons() {
        const { setVisiable, addCptVisible, plantableStore1, canEditCpt } = this.props.contentStore;
        const selectedLen = plantableStore1.selectedRowKeys.length;
        let btnArr = [];
        let that = this;
        if (canEditCpt) {
            btnArr.push(
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@biv7ox@add`} type="primary" onClick={() => {
                    this.props.contentStore.initInventrorySearchDatas({ isdata: 2 });
                    if (!addCptVisible) {
                        setVisiable(true, 5)
                    }
                }}> {getLabel(83476, '添加') } </Button>
            );
            btnArr.push(
                <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@squ063@batchDelete`} type="primary"
                    disabled={selectedLen == 0}
                    style={{ marginRight: "15px" }}
                    onClick={() => {
                        this.doDelete();
                    }}> {getLabel(32136, '批量删除')} </Button>
            );
        }
        return btnArr;
    }

    getTopButtons1() {
        const {plantableStore2, doCptAdd,doCptAddAll,listid } = this.props.contentStore;
        const selectedLen = plantableStore2.selectedRowKeys.length;
        let addCptButtons = [
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@0jkqby@add`} type="primary" disabled={selectedLen <= 0} onClick={() => doCptAdd({ listid: listid })} >{getLabel('504072','选择并添加') }</Button>),
            (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@kboah4@addAll`} type="primary" style={{ marginRight: "15px" }} onClick={() => doCptAddAll({ listid: listid })} >{getLabel('83786','全部添加') }</Button>),
        ]
        return addCptButtons;
    }

    getCptCondition = () => {
        const { conditionlistcpt, cptform } = this.props.contentStore;
        const { isFormInit } = cptform;
        let group = [];
        const formParamsTemp = cptform.getFormParams();
        for(let param in formParamsTemp){
            this.formParams[param] = formParamsTemp[param];
        }
        isFormInit && conditionlistcpt.map((c,i) => {
            let items = [];
            c.items.map((fields, index) => {
                items.push({
                    com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@kk1c5a@${fields.domkey[0]}`}
                        label={`${fields.label}`}
                        labelCol={{ span: `${fields.labelcol}` }}
                        wrapperCol={{ span: `${fields.fieldcol}` }}>
                        <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@81vc29@${fields.domkey[0]}`} fieldConfig={fields} form={cptform} formParams={this.formParams} onEnterSearch={this.onEnterSearch} />
                    </WeaFormItem>),
                    colSpan: 1,
                })
            });
            group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@fzyig8@${i}`} needTigger={true} title={c.title} showGroup={c.defaultshow} items={items} />)
        });
        return group;
    }

    getCondition = () => {
        const { conditionSerch, cptSerchform } = this.props.contentStore;
        const { isFormInit } = cptSerchform;
        let group = [];
        const formParamsTemp = cptSerchform.getFormParams();
        for(let param in formParamsTemp){
            this.formParams1[param] = formParamsTemp[param];
        }
        isFormInit && conditionSerch.map((c,i) => {
            let items = [];
            c.items.map((fields, index) => {
                items.push({
                    com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@8do761@${fields.domkey[0]}`}
                        label={`${fields.label}`}
                        labelCol={{ span: `${fields.labelcol}` }}
                        wrapperCol={{ span: `${fields.fieldcol}` }}>
                        <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@zbs68n@${fields.domkey[0]}`} fieldConfig={fields} form={cptSerchform} formParams={this.formParams1} onEnterSearch={this.onEnterSearch1} />
                    </WeaFormItem>),
                    colSpan: 1,
                })
            });
            group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@zbheid@${i}`} needTigger={true} title={c.title} showGroup={c.defaultshow} items={items} />)
        });
        return group;
    }

    closeAddCptDialog() {
        const { setVisiable } = this.props.contentStore;
        setVisiable(false, 5);
    }

    getFormFields() {
        const { contentStore } = this.props;
        const { basicInfo1, planlistForm1 } = contentStore;
        const { isFormInit } = planlistForm1;
        let group = [];
        //  const formParams = instockForm.getFormParams(); 
        isFormInit && basicInfo1.map((c,i) => {
            let items = [];
            c.items.map(fields => {
                let hide = false;
                let hasunderline = false;
                //只读样式控制
                if (fields.viewAttr == "1" || (fields.conditionType == "BROWSER" && fields.browserConditionParam.viewAttr == "1")) {
                    hasunderline = true
                }
                if (hasunderline) {
                    items.push({
                        com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@xdf3kw@${fields.domkey[0]}`}
                            label={`${fields.label}`}
                            labelCol={{ span: `${fields.labelcol}` }}
                            wrapperCol={{ span: `${fields.fieldcol}` }} underline>
                            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@9fv7h4@${fields.domkey[0]}`} fieldConfig={fields} form={planlistForm1} />
                        </WeaFormItem>),
                        colSpan: 1,
                        hide: hide
                    });
                } else {
                    items.push({
                        com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@o5jta5@${fields.domkey[0]}`}
                            label={`${fields.label}`}
                            labelCol={{ span: `${fields.labelcol}` }}
                            wrapperCol={{ span: `${fields.fieldcol}` }} >
                            <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@ayb3oz@${fields.domkey[0]}`} fieldConfig={fields} form={planlistForm1} />
                        </WeaFormItem>),
                        colSpan: 1,
                        hide: hide
                    });
                }

            });
            group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@0r40wh@${i}`} needTigger={true} title={c.title} col={2} showGroup={c.defaultshow} items={items} />)
        });
        return group;
    }

    onOperatesClick = (record, index, operate, flag, argumentString) => {
        const { doDeleteCpt, selectid, maintitle } = this.props.contentStore;
        let _href = operate && operate.href ? operate.href : "";
        let fn = _href.replace("javascript:", "");
        fn = fn.substring(0, fn.indexOf('('));
        let that = this;
        if (fn != "") {
            if ("onDel" == fn) {
                Modal.confirm({
                    title: getLabel(15172, '系统提示'),
                    content: getLabel(83600, '您确认要删除吗?'),
                    onOk() {
                        doDeleteCpt(record.randomFieldId, selectid, maintitle);
                    },
                    onCancel() { },
                });
            }
        }
    }

    getRightMenu() {
        const { plantableStore1, canEditCpt } = this.props.contentStore;
        const selectedLen = plantableStore1.selectedRowKeys.length;
        let btns = [];
        if (canEditCpt) {
            btns.push({
                key: "create",
                icon: <i className='icon-coms-Batch-add' />,
                content: getLabel(83476, '添加') ,
                onClick: key => {
                    this.props.contentStore.initInventrorySearchDatas({ isdata: 2 });
                    const { addCptVisible, setVisiable } = this.props.contentStore;
                    if (!addCptVisible) {
                        setVisiable(true, 5)
                    }
                }
            });
            btns.push({
                key: "batchDelete",
                disabled: selectedLen == 0,
                icon: <i className='icon-coms-delete' />,
                content: getLabel(32136, '批量删除'),
                onClick: key => {
                    this.doDelete();
                }
            });
        }
        return btns;
    }

    getRightMenu1() {
        const {plantableStore2,listid,doCptAdd, doCptAddAll } = this.props.contentStore;
        let selectedLen = plantableStore2.selectedRowKeys.length;
        let btns = [];
        btns.push({
            key: "seladd",
            disabled: selectedLen == 0,
            icon: <i className='icon-coms-Batch-add' />,
            content: getLabel(504072, '选择并添加') ,
            onClick: () => {
                doCptAdd({ listid: listid })
            }
        });
        btns.push({
            key: "alladd",
            icon: <i className='icon-coms-Batch-add' />,
            content: getLabel(83786, '全部添加'),
            onClick: () => {
                doCptAddAll({ listid: listid })
            }
        });
        return btns;
    }

    doDelete() {
        const { plantableStore1, selectid, maintitle } = this.props.contentStore;
        const { selectedRowKeys } = plantableStore1;//得到选中数据的id
        const count = toJS(selectedRowKeys).length;
        let contentStore = this.props.contentStore;
        Modal.confirm({
            title: getLabel(15172, '系统提示'),
            content: getLabel(83600, '您确认要删除吗?'),
            onOk() {
                contentStore.doDeleteCpt(selectedRowKeys, selectid, maintitle);
            },
            onCancel() { },
        });
    }

    onEnterSearch = () =>{
        const { handlePlanList, setCptSearchAd, listid, listtitle } = this.props.contentStore;
        handlePlanList(true, { id: listid, name: listtitle }, 0);
        setCptSearchAd(false);
    }

    onEnterSearch1 = () =>{
        const { doCptSearch, listid } = this.props.contentStore;
        doCptSearch({ listid: listid });
    }

}

export default PlanListInfoDialog;